//
//  MainTabBarController.m
//  MiAiApp
//
//  Created by voidcat on 2017/5/18.
//  Copyright © 2017年 voidcat. All rights reserved.
//

#import "MainTabBarController.h"

#import "RootNavigationController.h"
#import "HomeViewController.h"
#import "WaterFallListViewController.h"
#import "PersonListViewController.h"
#import "MakeFriendsViewController.h"
#import "MsgViewController.h"
#import "MineViewController.h"
#import "ToolDemoViewController.h"
#import "DraggingCardViewController.h"
#import "UITabBar+CustomBadge.h"
#import "HomeListViewController.h"
#import "IndexViewController.h"
#import "CYMineViewController.h"
#import "VideoChatViewController.h"
#import "ShortVideoViewController.h"
#import "XYTabBar.h"
#import "ConversationListViewController.h"
#import "WomenChatViewRootViewController.h"
@interface MainTabBarController ()<UITabBarControllerDelegate>

@property (nonatomic,strong) NSMutableArray * VCS;//tabbar root VC

@end

@implementation MainTabBarController


- (void)viewDidLoad {
    [super viewDidLoad];
    self.delegate = self;
    //初始化tabbar
    [self setUpTabBar];
    //添加子控制器
    [self setUpAllChildViewController];
//    [self upadteUnread];
//
//    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//        [self upadteUnread];
//    });
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}

- (void)upadteUnread {
    NSInteger unRead = [IMAPlatform sharedInstance].conversationMgr.unReadMessageCount;
    if(unRead == 0)
    {
        [self.tabBar setBadgeStyle:kCustomBadgeStyleNone value:unRead atIndex:2];

    }
    else
    {
        [self.tabBar setBadgeStyle:kCustomBadgeStyleNumber value:unRead atIndex:2];
    }
    
//    self.navigationController.tabBarItem.badgeValue = badge;
}

#pragma mark ————— 初始化TabBar —————
-(void)setUpTabBar{
    //设置背景色 去掉分割线
    [self setValue:[XYTabBar new] forKey:@"tabBar"];
    [self.tabBar setBackgroundColor:[UIColor whiteColor]];
    [self.tabBar setBackgroundImage:[UIImage new]];
    //通过这两个参数来调整badge位置
    //    [self.tabBar setTabIconWidth:29];
    //    [self.tabBar setBadgeTop:9];
}

#pragma mark - ——————— 初始化VC ————————
-(void)setUpAllChildViewController{
    _VCS = @[].mutableCopy;
    IndexViewController *homeVC = [[IndexViewController alloc]init];
    [self setupChildViewController:homeVC title:ASLocalizedString(@"首页")imageName:@"tabBar_Ranking" seleceImageName:@"tabBar_Ranking_selected"];
//
//    if(curUser.sex == UserGenderFemale)
//    {
//        WomenChatViewRootViewController *makeFriendVC = [[WomenChatViewRootViewController alloc]init];
//        [self setupChildViewController:makeFriendVC title:ASLocalizedString(@"视频聊")imageName:@"tabBar_video" seleceImageName:@"tabBar_video_selected"];
//    }
//    else
//    {
//        VideoChatViewController *makeFriendVC = [[VideoChatViewController alloc]init];
//        [self setupChildViewController:makeFriendVC title:ASLocalizedString(@"视频聊")imageName:@"tabBar_video" seleceImageName:@"tabBar_video_selected"];
//    }

//    ShortVideoViewController *msgVC = [ShortVideoViewController new];
//    [self setupChildViewController:msgVC title:ASLocalizedString(@"小视频")imageName:@"tabBar_shortVideo" seleceImageName:@"tabBar_shortVideo_selected"];
    
    MsgViewController *msgVCC = [[MsgViewController alloc]init];
    [self setupChildViewController:msgVCC title:ASLocalizedString(@"消息")imageName:@"tabBar_message" seleceImageName:@"tabBar_message_selected"];
    
    CYMineViewController *mineVC = [[CYMineViewController alloc]init];
    [self setupChildViewController:mineVC title:ASLocalizedString(@"我的")imageName:@"tabBar_mine" seleceImageName:@"tabBar_mine_selected"];
    
    self.viewControllers = _VCS;
}

-(void)setupChildViewController:(UIViewController*)controller title:(NSString *)title imageName:(NSString *)imageName seleceImageName:(NSString *)selectImageName{
    controller.title = title;
    controller.tabBarItem.title = title;//跟上面一样效果
    controller.tabBarItem.image = [[UIImage imageNamed:imageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    controller.tabBarItem.selectedImage = [[UIImage imageNamed:selectImageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    //未选中字体颜色
    [controller.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:KBlackColor,NSFontAttributeName:SYSTEMFONT(10.0f)} forState:UIControlStateNormal];
    
    //选中字体颜色
    [controller.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:MainColor,NSFontAttributeName:SYSTEMFONT(10.0f)} forState:UIControlStateSelected];
    //包装导航控制器
    RootNavigationController *nav = [[RootNavigationController alloc]initWithRootViewController:controller];
    
//    [self addChildViewController:nav];
    [_VCS addObject:nav];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onNewC2CMessage) name:KOnNewC2CMessage object:nil];
    
}

- (void)onNewC2CMessage {
    [self upadteUnread];
//    [self setRedDotWithIndex:3 isShow:YES];
}

-(void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
//    [self setRedDotWithIndex:tabBarController.selectedIndex isShow:NO];
    
    [self upadteUnread];
    //    NSLog(ASLocalizedString(@"选中 %ld"),tabBarController.selectedIndex);
}

-(void)setRedDotWithIndex:(NSInteger)index isShow:(BOOL)isShow{
    if (isShow) {
        [self.tabBar setBadgeStyle:kCustomBadgeStyleRedDot value:0 atIndex:index];
    }else{
        [self.tabBar setBadgeStyle:kCustomBadgeStyleNone value:0 atIndex:index];
    }
}

- (BOOL)shouldAutorotate {
    return [self.selectedViewController shouldAutorotate];
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations{
    return [self.selectedViewController supportedInterfaceOrientations];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
